package cn.nnxy.zentao.controller;

import cn.nnxy.zentao.domain.ZPublish;
import cn.nnxy.zentao.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/product")
public class publishController {
    @Autowired
    private PublishService publishService;

    @GetMapping("publish")
    public String publishListUI() {
        return "";
    }

    @ResponseBody
    @PostMapping("publish")
    public Map<String, Object> publishList(){
        Integer pageSize = 10;
        return null;
    }

    //新建publish
    @GetMapping("publishAdd")
    public String publishAddUI(){
        return "";
    }

    @PostMapping("publishAdd")
    public String publishAdd(ZPublish publish, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("publish",publish);
            model.addAttribute("msg", result.getAllErrors().get(0).getDefaultMessage());
            return "";
        }
        int temp = publishService.savePublish(publish);
        if (temp == 1){
            model.addAttribute("msg", "添加成功！");
            return "redirect:";
        }else {
            model.addAttribute("msg", "添加失败！");
            return "";
        }

    }

    //删除publish
    @ResponseBody
    @PostMapping("publishDel")
    public boolean publishDel(Integer ids){
        if (null == ids ){
            return false;
        }
        return publishService.deleteById(ids) > 0;
    }

    //编辑publish
    @GetMapping("publishEdit")
    public String publishEditUI(Integer publishId, Model model){
        ZPublish publish = publishService.seletById(publishId);
        model.addAttribute("publish", publish);
        return "";
    }

    @PostMapping("publishEdit")
    public String publishEdit(ZPublish publish, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("publish", publish);
            model.addAttribute("msg", result.getAllErrors());
            return "";
        }
        int temp = publishService.updateById(publish);
        if (temp == 1){
            model.addAttribute("msg", "修改成功。");
            return "redirect:";
        }else {
            model.addAttribute("msg", "修改失败。");
            return "";
        }

    }
}
